﻿HmJS.$Import('core.fx');

/*
---
version: 1.0-20
description: TOC is a MooTools Plugin which turn a list of HTML headings into a Table of Content.
license: MIT-style license.
download: http://mootools.net/forge/p/TOC
source: http://github.com/thinkphp/TOC

authors:
- Adrian Statescu (http://thinkphp.ro).

requires:
core/1.2.1: '*'

provides: [TOC]
...
*/

HmJS.register('util.effect.ScrollSidebar', function ($ns) {

	/* class ScrollSidebar */
	return new Class({

		/* Implements */
		Implements: [Options],

		Binds: ['_action', '_destroy'],

		/* options */
		options: {

			offsets: { x: 0, y: 0 },

			mode: 'vertical',

			positionVertical: 'top',

			positionHorizontal: 'left',

			speed: 400
		},

		/* constructor of class - public method */
		//@param (String) ID of the menu bar
		//@param (Object) options for personalization
		initialize: function (menu, options) {

			this.setOptions(options);

			this.menu = $(menu);

			this.move = this.options.mode == 'vertical' ? 'y' : 'x';

			this.property = this.move == 'y' ? 'positionVertical' : 'positionHorizontal';

			var css = { position: 'absolute', 'display': 'block' };

			css[this.options.positionsVertical] = this.options.offsets.y;

			css[this.options.positionsHorizontal] = this.options.offsets.x;

			this.menu.setStyles(css);

			this.menu.set('tween', { duration: this.options.speed });

			this._setEvent('add');
		},

		/* private method */
		//@param move (int) 
		setPosition: function (move) {
			this.menu.tween(this.options[this.property], move);
			return this;
		},

		_action: function () {
			this.setPosition($(document.body).getScroll()[this.move] + this.options.offsets[this.move]);
		},

		_setEvent: function (motion) {
			var m = motion + 'Event';
			window[m]('scroll', this._action);
			window[m]('domready', this._action);
			window[m]('beforeunload', this._destroy);
		},

		_destroy: function () {
			this._setEvent('remove');
		}

	});

});